from django.db import models


# 数据校验
# 1）前端校验(JavaScript)
# 2) 后端校验(Django)
#       数据库层(Model)
#       表单层(Form)
from django.urls import reverse


class List(models.Model):

    def get_absolute_url(self):
        return reverse('view_list', args=[self.id])


class Item(models.Model):
    # 默认blank=False表单提交不能为空
    text = models.TextField(default='')
    # 级联删除，如果列表被删除，列表内的项都会被删除
    list = models.ForeignKey(List, on_delete=models.CASCADE)

    class Meta:
        ordering = ('id',)
        unique_together = ('list', 'text')

    def __str__(self):
        return self.text
